#include "view_time_6.h"
#include "view_time_6.inc"
#include "view_time_0.h"
#include "display_ctrl.h"
#include "platform_assert.h"
#include "lfs_file_manager_auto.h"
#include "rtc.h"
#include "datetime.h"
#include "blecom.h"
#include "battery_ctrl.h"
#include "sport_his.h"
#include "display_ctrl.h"
#include "display_interface_240x240.h"
#include "datetime.h"
#include "heart_rate_interface.h"
#include "font/num_font.h"
#include "font_mngr.h"
#include "view_common.h"
#include "app_trace.h"
#include "weather_ctrl.h"
#include "view_times.h"
#include "lfs_file_manager.h"
#include "stopwatch.h"

#include "app_trace.h"
#define LOG(fmt, ...) app_trace_log(fmt "\r\n", ##__VA_ARGS__)

#define DISP_TIME_COM_DATA_MONTH (disp_ctrl.com_data.uint8_temp[1])
#define DISP_TIME_COM_DATA_DAY (disp_ctrl.com_data.uint8_temp[2])
#define DISP_TIME_COM_DATA_HOUR (disp_ctrl.com_data.uint8_temp[3])
#define DISP_TIME_COM_DATA_MINUTE (disp_ctrl.com_data.uint8_temp[4])
#define DISP_TIME_COM_DATA_WEEK (disp_ctrl.com_data.uint8_temp[5])
#define DISP_TIME_COM_DATA_BLE_IF_CONNEDT (disp_ctrl.com_data.uint8_temp[6])
#define DISP_TIME_COM_DATA_BATTERT_PERCENT (disp_ctrl.com_data.uint8_temp[7])
#define DISP_TIME_COM_DATA_BATTERT_IF_CHARGE (disp_ctrl.com_data.uint8_temp[8])
#define DISP_TIME_COM_DATA_24_12 (disp_ctrl.com_data.uint8_temp[9])
static void _disp_show_time_get_com_data(void)
{
    datetime_t UIdatetime = DateSync();
    DISP_TIME_COM_DATA_MONTH = UIdatetime.date.month;
    DISP_TIME_COM_DATA_DAY = UIdatetime.date.day;
    DISP_TIME_COM_DATA_HOUR = UIdatetime.time.hour;
    DISP_TIME_COM_DATA_MINUTE = UIdatetime.time.minute;
    DISP_TIME_COM_DATA_WEEK = get_present_week();
    DISP_TIME_COM_DATA_BLE_IF_CONNEDT = ble_if_connected();
    DISP_TIME_COM_DATA_BATTERT_PERCENT = battery_ctrl_get_percent();
    DISP_TIME_COM_DATA_BATTERT_IF_CHARGE = battery_ctrl_if_charging();
    DISP_TIME_COM_DATA_24_12 = if_time_hour_system_12();

    if (DISP_TIME_COM_DATA_24_12)
    {
        DISP_TIME_COM_DATA_24_12 += (DISP_TIME_COM_DATA_HOUR >= 12); // 1=am,2=pm
        DISP_TIME_COM_DATA_HOUR %= 12;
        if (DISP_TIME_COM_DATA_HOUR == 0)
        {
            DISP_TIME_COM_DATA_HOUR = 12;
        }
    }
}

/*********************************************************************************************
 * @brief 画一个flash图片的环
 * @warning
 ********************************************************************************************/
static void _disp_show_flash_img_annulus(uint8_t file_index, int16_t x, int16_t y, uint8_t img_sn, annulus_cfg_t *p_cfg)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        if (lfs_file_open(&little_fs, &disp_ctrl.common_file[file_index], lfs_file_get_name(img_sn), LFS_O_RDONLY) == LFS_ERR_OK)
        {
            uint8_t buff[4];
            if (lfs_file_read(&little_fs, &disp_ctrl.common_file[file_index], buff, sizeof(buff)) > 0)
            {
                disp_ctrl.common_file_w[file_index] = (buff[1] << 8) | buff[0];
                disp_ctrl.common_file_h[file_index] = (buff[3] << 8) | buff[2];
            }
            if ((disp_ctrl.common_file_w[file_index] > OLED_SIZE_X) || (disp_ctrl.common_file_h[file_index] > OLED_SIZE_Y))
            {
                disp_ctrl.common_file_w[file_index] = 0;
                disp_ctrl.common_file_h[file_index] = 0;
            }
        }
        else
        {
            disp_ctrl.common_file_h[file_index] = 0xFF;
        }
    }
    disp_draw_flash_image_annulus(x, y, 0, disp_ctrl.common_file_w[file_index], disp_ctrl.common_file_h[file_index], &disp_ctrl.common_file[file_index], p_cfg);
}

uint32_t get_animation(void)
{
    uint32_t const time = stopwatch_get_catch_time(0);
    uint32_t const cycle_time = 4000;
    int const animation_frame = time % (cycle_time);

    // uint32_t const frame_total = sizeof(earth_animation_TAB) / sizeof(earth_animation_TAB[0]);
    uint32_t const frame_total = FILE_IMG_z001_1_00117 - FILE_IMG_z001_1_00000 + 1;
    int frame_index = frame_total * animation_frame / cycle_time;
    // uint8_t const *bmp = earth_animation_TAB[frame_index];
     //LOG("%u", frame_index);
    return FILE_IMG_z001_1_00000 + frame_index;
    // return FILE_IMG_z001_1_00000;
}
void view_time_6_display(void)
{
    annulus_cfg_t *p_battery_annulus_cfg = (annulus_cfg_t *)&disp_ctrl.com_data.i_temp[50];
    bool is_refresh_all = false;
    if (disp_ctrl.com_data.i_temp[42] == 2)
    {
        is_refresh_all = true;
    }
    if (disp_ctrl.loop_cnt == 0)
    {
        bool is_next_refresh_all = false;
        if (disp_ctrl.com_data.i_temp[42] == 1)
        {
            // LOG("refresh start");
            disp_ctrl.com_data.i_temp[42] = 2;
            is_refresh_all = true;
        }
        else if (disp_ctrl.com_data.i_temp[42] == 2)
        {
            // LOG("refresh end");
            disp_ctrl.com_data.i_temp[42] = 0;
        }
        else if (disp_ctrl.com_data.i_temp[42] == 0)
        {
            uint16_t temp16 = get_present_day_minute();
            disp_ctrl.com_data.i_temp[5] = (temp16 / 2) % 360; //时针的角度
            disp_ctrl.com_data.i_temp[6] = (temp16 * 6) % 360; //分针的角度
            if (disp_ctrl.com_data.i_temp[41] != disp_ctrl.com_data.i_temp[6])
            {
                is_next_refresh_all = true;
                disp_ctrl.com_data.i_temp[42] = 1;
                disp_ctrl.com_data.i_temp[41] = disp_ctrl.com_data.i_temp[6];
            }
        }
        if (is_next_refresh_all)
        {
            // LOG("refresh request");
            disp_ctrl.area_update_flag = 0;
            disp_ctrl.com_data.i_temp[42] = 1;
        }
        else
        {
            display_set_update_area(82 + 9, 133 + 11, 55, 55);
        }

        // disp_ctrl.auto_refresh_time = 1000;
        disp_ctrl.auto_refresh_time = 100;
        disp_ctrl.com_data.i_temp[40] = get_animation();
        if (is_refresh_all)
        {
            _disp_show_time_get_com_data();
            //天气类型
            weather_record_t weather;
            weather_ctrl_get(0, &weather);
            disp_ctrl.com_data.uint8_temp[12] = weather.type % WEATHER_TYPE_END;
            if (disp_ctrl.com_data.uint8_temp[12] == WEATHER_TYPE_UNKNOW)
            {
                disp_ctrl.com_data.uint8_temp[12] = WEATHER_TYPE_CLOUDY;
            }

            // disp_ctrl.com_data.i_temp[7] = (RTCGetSeconds() % 60) * 6; //秒针

            //电量环
            p_battery_annulus_cfg->centre_x = 146 + 27;
            p_battery_annulus_cfg->centre_y = 92 + 27;
            p_battery_annulus_cfg->r1 = 22;
            p_battery_annulus_cfg->r2 = 27;
            disp_annulus_calc((DISP_TIME_COM_DATA_BATTERT_PERCENT * 36) / 10, p_battery_annulus_cfg);

            //
            disp_ctrl.com_data.i_temp[10] = hr_get_hr_data();
            disp_ctrl.com_data.i_temp[11] = sport_get_present_step();
            disp_ctrl.com_data.i_temp[12] = sport_get_calorie() / 1000;
            disp_ctrl.com_data.i_temp[13] = sport_get_distance() / 1000;
        }
    }
    if (is_refresh_all)
    {
        // LOG("refresh %d", disp_ctrl.area_start_y);
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 9, 11, FILE_IMG_TIME_APPLE_BG_9_11); // 82 133
    }
    // disp_draw_user_image_ignore_color(82 + 9, 133 + 11, RGB565_COLOR_BLACK, disp_ctrl.com_data.p_temp[40]);
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_2, 82 + 9, 133 + 11, disp_ctrl.com_data.i_temp[40]);
    if (is_refresh_all)
    {

        //电池环+格数 168,111---176,130,背景满格电量，空的电量涂黑
        _disp_show_flash_img_annulus(DISP_LFS_FILE_INDEX_1, 146, 92, FILE_IMG_TIME_APPLE_BATTERY_CIRCLE, p_battery_annulus_cfg);
        int offset = (100 - DISP_TIME_COM_DATA_BATTERT_PERCENT) * 20 / 100;
        disp_draw_rect_color(168, 111, 176, 111 + offset, RGB565_COLOR_BLACK);

        //蓝牙图标
        if (DISP_TIME_COM_DATA_BLE_IF_CONNEDT)
        {
            disp_draw_user_image(114, 224, IMAGE_USER_BLE_NEW_15X15);
        }
        //天气
        uint16_t color = RGB565_COLOR_WHITE;
        if ((disp_ctrl.com_data.uint8_temp[12] == WEATHER_TYPE_SUNSHINE) || (disp_ctrl.com_data.uint8_temp[12] == WEATHER_TYPE_HAZE))
        {
            color = RGB565(252, 209, 53);
        }
        disp_draw_user_image_gray_lucency(47, 101, color, IMAGE_USER_APPLE_TIME1_WEATHER_TAB[disp_ctrl.com_data.uint8_temp[12] - 1]);

        //日期+星期
        disp_draw_user_image_ignore_color(111, 73, 0, IMAGE_USER_NUM8X12_TAB[DISP_TIME_COM_DATA_DAY / 10]);
        disp_draw_user_image_ignore_color(120, 73, 0, IMAGE_USER_NUM8X12_TAB[DISP_TIME_COM_DATA_DAY % 10]);
        disp_show_ascii_string(110, 52, (char *)WEEK_STRING[DISP_TIME_COM_DATA_WEEK - 1], 16, RGB565_COLOR_RED);
    }
    //指针
    disp_draw_img_rotate(gImage_apple_time1_shizhen, 0, 65, (119 << 16) | 119, disp_ctrl.com_data.i_temp[5]);
    disp_draw_img_rotate(gImage_apple_time1_fenzhen, 0, 85, (119 << 16) | 119, disp_ctrl.com_data.i_temp[6]);
    // disp_draw_img_rotate(gImage_apple_time1_miaozhen, 0, 98, (119 << 16) | 119, disp_ctrl.com_data.i_temp[7]);
    if (is_refresh_all)
    {
        disp_draw_user_image_ignore_color(112, 112, 0, IMAGE_USER_APPLE_TIME1_CIRCLE);
        //卡路里
        _disp_show_num_extend(DISP_NUM_TYPE_LEFT, 6, 8, disp_ctrl.com_data.i_temp[12], 8, IMAGE_USER_NUM8X12_TAB);

        //心率
        _disp_show_num_extend(DISP_NUM_TYPE_RIGHT, 230, 8, disp_ctrl.com_data.i_temp[10], 8, IMAGE_USER_NUM8X12_TAB);

        //距离
        _disp_show_num_extend(DISP_NUM_TYPE_LEFT, 6, 218, disp_ctrl.com_data.i_temp[13], 8, IMAGE_USER_NUM8X12_TAB);

        //步数
        _disp_show_num_extend(DISP_NUM_TYPE_RIGHT, 230, 218, disp_ctrl.com_data.i_temp[11], 8, IMAGE_USER_NUM8X12_TAB);
    }
}
